<template>
  <el-card>
    <div>
      <!-- 1.0 行内的表单查询 -->
      <el-form
        size="mini"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="订单号">
          <el-input
            v-model="searchForm.orderNo"
            placeholder="订单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input
            v-model="searchForm.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchForm.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchForm.orderState" placeholder="订单状态">
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-date-picker
            v-model="searchForm.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 2.0 表格的数据渲染 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="orderNo" label="订单号" width="120">
        </el-table-column>
        <el-table-column label="下单时间" width="180">
          <template v-slot="{ row }">
            {{ row.orderTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="120">
        </el-table-column>
        <el-table-column label="送达时间" width="180">
          <template v-slot="{ row }">
            {{ row.deliveryTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="120">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="110">
          <template v-slot="{ row }">
            <el-button type="text" size="mini" @click="showCur(row)"
              >查看</el-button
            >
            <!-- @click="$router.push('/order/edit?id=' + row.id)" -->
            <!-- $router.push({ path: '/order/edit', query: { id: row.id } }) -->
            <!-- @click="$router.push('/order/edit/' + row.id)" -->
            <el-button
              type="text"
              size="mini"
              @click="$router.push('/order/edit/' + row.id)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.0 分页处理 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 查看订单 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <div>
          {{ curData }}

          <div style="height: 300px;">
            <el-steps direction="vertical" :active="getState">
              <el-step title="已受理"></el-step>
              <el-step title="派送中"></el-step>
              <el-step title="已完成"></el-step>
            </el-steps>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { getOrders, searchOrders } from '@/api/orders.js'
import _ from 'lodash'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      // 查询表单
      searchForm: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      dialogVisible: false,
      curData: {}
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data, total } = await getOrders({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm
      })
      // console.log(data)
      this.tableData = data
      this.total = total
    },
    async onSubmit() {
      // 表单的数据如果在发请求的时候需要处理格式，一定要先拷贝
      const obj = _.cloneDeep(this.searchForm)
      obj.date = JSON.stringify(obj.date)
      this.currentPage = 1
      const { data, total } = await searchOrders({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...obj
      })
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getOrderList()
    },
    showCur(row) {
      console.log(this.getState)
      this.dialogVisible = true
      this.curData = row
    }
  },
  computed: {
    getState() {
      let num = 1
      switch (this.curData.orderState) {
        case '已受理':
          num = 1
          break
        case '派送中':
          num = 2
          break
        case '已完成':
          num = 3
          break
      }
      return num
    }
  }
}
</script>

<style lang="less" scoped></style>
